<?php
Class MembershipModel extends Model{
	public function __construct(){
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
	public function GetPostingPoint($table="",$field="id",$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($table,$field,$where);
	}
	public function GetEmpId($table="",$field="id",$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($table,$field,$where);
	}
	public function GetSubscription($table="",$field="id",$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($table,$field,$where);
	}
	public function EditMembership($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields,$where);
	}
	public function getData($emp_id){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Memberships WHERE user_id='".$emp_id."'";
		$result = parent::query($query);
		$datas = mysql_fetch_object($result);
		return $datas;
	}
	public function getMemberShipPoint($emp_id){
		$this->open(Model::MYSQL);
		$query = "SELECT posting_point FROM Memberships WHERE user_id='".$emp_id."' AND sub_type='Employer' ";
		$result = parent::query($query);
		$datas = mysql_fetch_object($result);
		return $datas->posting_point;
	}
	public function save($field=array()){
		$this->open(Model::MYSQL);
		return parent::save($field);
	}
	public function update($field=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($field,$where);
	}
	public function delete($where=array(),$deleteSet=false){
		$this->open(Model::MYSQL);
		return parent::delete($where,$deleteSet);
	}
	public function findObject($where=array()){
		$this->open(Model::MYSQL);
		return parent::findAsObject($where);
	}
	public function getObject($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::getAsObject($fields,$where);
	}
	public function version($table="",$field="id",$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($table,$field,$where);
	}	
	public function InsertMembership($emp_id,$field){
		$this->open(Model::MYSQL);
		$date = date("Y-m-d H:i:s");
		$date = strtotime($date);
		$date = strtotime("+" . $field->duration. " day" , $date);
		$date = date('Y-m-d H:i:s', $date);
		$querymembership = "INSERT INTO Memberships (user_id,subscription_start,subscription_expire,version,created,sub_type) VALUES ($emp_id,NOW(),'$date',1,NOW(),'Employer');";
		parent::query($querymembership);
	}
	
	public function get_follow_resume_list($emp_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Resumes.* FROM Resumes LEFT JOIN Memberships ON Resumes.id = Memberships.resume_id WHERE user_id=$emp_id ";
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getPage($can_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT DISTINCT cat_id,Memberships.id as mid, Memberships.user_id,Memberships.created
				  FROM Memberships
				  WHERE Memberships.sub_type='Candidate' AND Memberships.user_id = $can_id AND cat_id !='NULL' 
				  Group by Memberships.cat_id
					" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getPage1($can_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT DISTINCT Memberships.loc_id, Memberships.id as mid, Memberships.user_id,Memberships.created
		FROM Memberships
		WHERE Memberships.sub_type='Candidate' AND Memberships.user_id = $can_id AND loc_id !='NULL' 
		Group by Memberships.loc_id
		" ;
		if(!empty($sortFields)){
		$tempSortFields=array();
		foreach($sortFields AS $field ){
		$tempSortFields[]= " `$field` " ;
		}
		$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
		$rows = $result ;
		$result = array();
		while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getPage2($can_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT DISTINCT Memberships.time_id, Memberships.id as mid, Memberships.user_id,Memberships.created
		FROM Memberships
		WHERE Memberships.sub_type='Candidate' AND Memberships.user_id = $can_id AND time_id !='NULL'   
		Group by Memberships.time_id
		" ;
		if(!empty($sortFields)){
		$tempSortFields=array();
		foreach($sortFields AS $field ){
		$tempSortFields[]= " `$field` " ;
		}
		$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
		$rows = $result ;
		$result = array();
		while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getDataWithCate($id){
		$this->open(MODEL::MYSQL);
		$query = "SELECT * FROM Memberships where user_id=$id ";
		$result=parent::query($query);
		$sRows=array();
		$rows=array();
		while(($row=mysql_fetch_object($result))){
			$ids = explode(",",$row->cat_id);
			foreach($ids as $id){
				$query = "SELECT * from Categories WHERE Categories.id = $id" ;
				$sResult=parent::query($query);
				while(($sRow=mysql_fetch_object($sResult)))$sRows=$sRow;
			}
			$row->cat_id=$sRows;
			$rows[]=$row;
		}
		return $rows;
	}
	public function getCandidateMemberships($can_id){
		$this->open(MODEL::MYSQL);
		$query = "SELECT * FROM Memberships WHERE user_id = $can_id AND sub_type = 'Candidate'  ";
		$result=parent::query($query);
		$sRows=array();
		$rows=array();
		while(($row=mysql_fetch_object($result)))$rows[$row->cat_id]=$row;
		return $rows;
	}
	public function getMembershipsCate($can_id){
		//$this->open(MODEL::MYSQL);
		$query = "SELECT cat_id FROM Memberships WHERE user_id = $can_id AND sub_type = 'Candidate'  ";
		$rows=array();
		$result=parent::query($query);
		while(($row=mysql_fetch_object($result))){
			$rows[$row->cat_id]=$row;
		}
		return $rows;
	}
}
